package com.carter.springframework.core.annotation;

import java.lang.annotation.Annotation;

/**
 * 注解过滤器
 */
public interface AnnotationFilter {
    /**
     * 类是否属于java.lang、org.springframework.lang包
     */
    AnnotationFilter PLAIN = packages("java.lang", "org.springframework.lang");

    /**
     * 类是否属于java、javax包
     */
    AnnotationFilter JAVA = packages("java", "javax");

    /**
     * 任何类
     */
    AnnotationFilter ALL = new AnnotationFilter() {
        @Override
        public boolean matches(Annotation annotation) {
            return true;
        }
        @Override
        public boolean matches(Class<?> type) {
            return true;
        }
        @Override
        public boolean matches(String typeName) {
            return true;
        }
        @Override
        public String toString() {
            return "All annotations filtered";
        }
    };

    /**
     * {@link AnnotationFilter} that never matches and can be used when no
     * filtering is needed (allowing for any annotation types to be present).
     * @deprecated as of 5.2.6 since the {@link MergedAnnotations} model
     * always ignores lang annotations according to the {@link #PLAIN} filter
     * (for efficiency reasons)
     * @see #PLAIN
     */
    @Deprecated
    AnnotationFilter NONE = new AnnotationFilter() {
        @Override
        public boolean matches(Annotation annotation) {
            return false;
        }
        @Override
        public boolean matches(Class<?> type) {
            return false;
        }
        @Override
        public boolean matches(String typeName) {
            return false;
        }
        @Override
        public String toString() {
            return "No annotation filtering";
        }
    };


    /**
     * 根据实例匹配
     */
    default boolean matches(Annotation annotation) {
        return matches(annotation.annotationType());
    }

    /**
     * 根据类型进行匹配
     */
    default boolean matches(Class<?> type) {
        return matches(type.getName());
    }

    /**
     * 根据名称进行匹配
     */
    boolean matches(String typeName);


    /**
     * Create a new {@link AnnotationFilter} that matches annotations in the
     * specified packages.
     * @param packages the annotation packages that should match
     * @return a new {@link AnnotationFilter} instance
     */
    static AnnotationFilter packages(String... packages) {
        return new PackagesAnnotationFilter(packages);
    }
}
